package com.pku.jtestx86.popup.popup.actions;

import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;

import com.pku.jtestx86.views.CFGView;
import com.pku.jtestx86.views.PDGView;
import com.pku.ss.ui.PDGExplorer;

public class PDGAction implements IObjectActionDelegate {

	private Shell shell;
	private IWorkbenchPart targetPart;
	@Override
	public void run(IAction arg0) {
		// TODO Auto-generated method stub
		TreeSelection selection=(TreeSelection) this.targetPart.getSite().getSelectionProvider().getSelection();
		int segmentCount=selection.getPaths()[0].getSegmentCount();
		org.eclipse.jdt.internal.core.CompilationUnit compilationUnit=(org.eclipse.jdt.internal.core.CompilationUnit) selection.getPaths()[0].getSegment(segmentCount-1);
		try {
			CompilationUnit node = compilationUnit.reconcile(
					AST.JLS4, true, null, null);
			PDGView viewPart;
			try {
				viewPart = (PDGView) this.targetPart.getSite().getWorkbenchWindow().getActivePage().showView("com.pku.jtestx86.views.PDGView");
				viewPart.show(node);
			} catch (PartInitException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//PDGExplorer.Init(node);
		} catch (JavaModelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//CFGExplorer.Init(compilationUnit);
	}

	@Override
	public void selectionChanged(IAction arg0, ISelection arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// TODO Auto-generated method stub
		shell = targetPart.getSite().getShell();
		this.targetPart=targetPart;
	}

}
